package per.wsj.paging;

import androidx.annotation.IntRange;

import java.util.ArrayList;
import java.util.List;

public class CustomRepository {
    List<StudentModel> studentModels = new ArrayList<>();

    CustomRepository() {
        for (int i = 0; i < 160; i++) {
            StudentModel studentModel = new StudentModel();
            studentModel.setId(i);
            studentModel.setName("name" + i);
            studentModels.add(studentModel);
        }
    }

    List<StudentModel> getStudentsInit(@IntRange(from = 0) int initSize) {
        return studentModels.subList(0, initSize);//包含~不包含
    }

    List<StudentModel> getStudentsByRange(@IntRange(from = 0) int from, int to) {
        return studentModels.subList(from, to);
    }

    List<StudentModel> getStudentsByPage(@IntRange(from = 0) int page, int size) {
        int totalPage = 0;
        if (studentModels.size() % size == 0) {
            totalPage = studentModels.size() / size;
        } else {
            totalPage = studentModels.size() / size + 1;
        }
        if (page >= totalPage || page < 0) {
            return null;
        }
        if (page == totalPage - 1) {
            return studentModels.subList(page * size, studentModels.size());
        }
        return studentModels.subList(page * size, (page + 1) * size);
    }
}
